// [文件名]   pcRu = '/Volumes/jacky/PROJECT/wo-store/',
const
    http = require("http"),
    fs = require("fs"),
    serverurl01 = '/Volumes/jacky/PROJECT/vueProject/html/',
    serverurl02 = '/Volumes/jacky/liantong-woyun/web_resources/wo-store/html/',
    serverurl03 = '/Volumes/jacky/PROJECT/redHtml/',
	serverurl04 = '/Volumes/jacky/web-sd/vueStudy/';

serverGo(serverurl01, 7000);
serverGo(serverurl02, 9000);
serverGo(serverurl03, 8000);
serverGo(serverurl04, 9001);

function serverGo(serverurl, listenPort) {
    const dru = serverurl;

    http.createServer((request, response) => { // 创建一个http的Server

        let
            Url = request.url.indexOf('?') > 0 ? request.url.slice(0, request.url.indexOf('?')) : request.url, // 获取端口号后面的路径

            fileUrl = dru + Url, // 进行绝对路径拼接
            txtType = fileUrl.slice(fileUrl.lastIndexOf('.') + 1); // 获取文件类型

        // txtType = txtType.slice(txtType.indexOf('.') + 1);
        // txtType = txtType.indexOf('?') > 0 ? txtType.substr(0, txtType.indexOf('?')) : txtType;
//        console.log(txtType);
        fs.readFile(fileUrl, (err, data) => { // 读取文件 传入路径参数 和 回调函数

            if (err) { // 遇到错误返回404页面
                response.writeHead(404, {
                    "Content-Type": "text/html;charset=\"utf-8\""
                });
                response.end('404'); // 页面输出404
            } else { // 返回正常
                response.writeHead(200, {
                    "Content-Type": "text/" + txtType + ";charset=\"utf-8\""
                        // 确定文件的解析方式 以及 编码格式
                });
                response.write(data); // 导出文件内容
                response.end(); //结束处理
            }

        });

    }).listen(listenPort); // 设定监听端口
}
